package hotel.action;

import hotel.service.ServiceManager;
import hotel.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 * This class change user role
 * @author Alex
 *
 */
public class ChangeRoleAction extends Action 
{
	private static final Logger logger = Logger.getLogger(ChangeRoleAction.class);
	private static final String CHANGEROLEACTION_ERROR = "Error in ChangeRoleAction:";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception 
	 {
		 String mapper = ForwardActionTo.ERROR;
		 try
		 {
			 int id;
			 boolean role;
			 id = Integer.valueOf(request.getParameter("id"));
			 role = Boolean.valueOf(request.getParameter("role"));
			 UserService userService = ServiceManager.getDefault().getUserService();
			 userService.changeRole(role, id);
			 mapper = ForwardActionTo.SUCCESS;
				
		 }
         catch(NullPointerException e)
		 {
		 	logger.error(CHANGEROLEACTION_ERROR + e);
		 }
		 
		 return mapping.findForward(mapper);
	 }
}
